package com.spt.springbootzhsx.service;

import com.spt.springbootzhsx.dao.MovieDao;
import com.spt.springbootzhsx.entity.Movie;
import com.spt.springbootzhsx.mapper.MovieMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class MovieImpl implements MovieService{
    @Autowired
    private MovieMapper movieMapper;

    @Override
    public ArrayList<Movie> getAllMovie() {
        return movieMapper.getAllMovie();
    }

    @Override
    public ArrayList<Movie> getMovieByMstate(String mstate) {
        return movieMapper.getMovieByMstate(mstate);
    }

    @Override
    public ArrayList<Movie> getTop5Movie() {
        ArrayList<Movie> list = movieMapper.getMovieByMstate("正在热映");
        list.sort((o1, o2) -> {
            float result = o1.getRate() - o2.getRate();
            return result > 0 ? -1 : 1;
        });
        if (list.size() > 5) {
            int i=0;
            ArrayList<Movie> list1=new ArrayList<>();
            for (Movie movie : list) {
                list1.add(movie);
                i++;
                if(i==5)break;
            }
            return list1;
        }
        return list;
    }

    @Override
    public ArrayList<Movie> getTop7Movie() {
        ArrayList<Movie> list = movieMapper.getMovieByMstate("正在热映");
        list.sort((o1, o2) -> {
            float result = o1.getRate() - o2.getRate();
            return result > 0 ? -1 : 1;
        });
        if (list.size() > 7) {
            int i=0;
            ArrayList<Movie> list1=new ArrayList<>();
            for (Movie movie : list) {
                list1.add(movie);
                i++;
                if(i==7)break;
            }
            return list1;
        }
        return list;
    }

    @Override
    public ArrayList<Movie> getMovieByMname(String mname) {
        return movieMapper.getMovieByMname(mname);
    }

    @Override
    public Movie getMovieByMid(Integer mid) {
        return movieMapper.getMovieByMid(mid);
    }

    @Override
    public ArrayList<Movie> getMovieByKeyAndMstate(String key, String mstate) {
        return movieMapper.getMovieByKeyAndMstate(key,mstate);
    }

    @Override
    public ArrayList<Movie> getMovieByMtype(String mtype) {
        return movieMapper.getMovieByMtype(mtype);
    }

    @Override
    public String addMovie(Movie movie) {
        movie.setPic("默认.jpg");
        int k=movieMapper.addMovie(movie);
        if(k>=1){
            return "电影添加成功！";
        }else{
            return "电影添加失败！";
        }
    }

    @Override
    public String updateMovie(Movie movie) {
        int k=movieMapper.updateMovie(movie);
        if(k>=1){
            return "电影更新成功！";
        }else{
            return "电影更新失败！";
        }
    }

    @Override
    public String deleteMovie(Integer mid) {
        int k=movieMapper.deleteMovie(mid);
        if(k>=1){
            return "电影删除成功！";
        }else{
            return "电影删除失败！";
        }
    }

    @Override
    public ArrayList<Movie> getMovieByIndex(Integer start, Integer num) {
        ArrayList<Movie> list=movieMapper.getAllMovie();
        int i=0;
        while(i<start) i++;
        ArrayList<Movie> mlist=new ArrayList<>();
        while(i<list.size()&&i<start+num){
            mlist.add(list.get(i));
            i++;
        }
        return mlist;
    }
}
